#!/bin/bash

git pull
source go-env.sh
go install jigsaw

portParam=9001
envParam=$1
if [ ! -n "$envParam" ]; then
	envParam="dev"
elif [ "$envParam" == "dev" ]; then
    portParam=11001
elif [ "$envParam" == "prod" ]; then
    portParam=9001
elif [ "$envParam" == "adminDev" ]; then
    portParam=11002
elif [ "$envParam" == "adminProd" ]; then
    portParam=9992
elif [ "$envParam" == "adminProdJigsaw" ]; then
    portParam=11001
fi

echo "stop jigsaw"

pid=`sudo netstat -nplt | grep jigsaw | grep ${portParam} | awk '{print $7}' | awk -F / '{print $1}'`
echo "old pid: ${pid}"
if [ ${pid} ]
then
	echo "kill pid ${pid}"
	kill -9 ${pid}
fi


echo "starting jigsaw in env:${envParam}, port:${portParam}"

nohup ./bin/jigsaw ${envParam} >./logs/jigsaw.log 2>&1 &
